package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Car;
import cn.qf.shop.pojo.qo.CarQo;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@EnableSwagger2
@RequestMapping("/car")
@Api(tags = "车辆管理业务接口")
@RestController
@RestControllerAdvice
public class CarController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(CarController.class);
     @Resource
    private CarService carService;
    @ResponseBody
    @ApiOperation("车辆信息展示")
    @PostMapping(value = "/selectAllBycar",produces = {"application/json;charset=UTF-8"})
    public RespResult selectAll(){
        List<Car> cars = carService.selectAll();
        RespResult respResult=null;
        if (cars==null){
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult= super.returnRespWithSuccess(cars);
        return respResult;
    }
    @ResponseBody
    @ApiOperation("根据关键字查询")
    @PostMapping(value = "/selectByKeyWord",produces = {"application/json;charset=UTF-8"})
    public RespResult selectByKeyWord(CarQo carQo){
        List<Car> cars = carService.selectByKeyWord(carQo);
        RespResult respResult=null;
        if (cars==null){
            respResult = super.returnRespWithFailed("找不到用户列表");
        }
        respResult= super.returnRespWithSuccess(cars);
        return respResult;
    }
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/updateByCarIds",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
     public RespResult updateByCarIds(@RequestParam(value = "carIds[]") Set<Integer> carIds){
        int i = carService.updateByCarIds(carIds);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("删除失败");
        }
        respResult=selectAll();

        return respResult;
    }
    @ResponseBody
    @ApiOperation("增加数据")
    @RequestMapping(value = "/AddCar",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult AddCar(CarQo carQo){
        int i = carService.AddCar(carQo);
        RespResult respResult=null;
        if (i==-1){
            respResult = super.returnRespWithFailed("添加失败");
        }
        respResult=selectAll();
        return respResult;
    }
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/updateByOnclickForCar",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByOnclickForCar(CarQo carQo){
        int i = carService.updateByOnclick(carQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("修改失败");
        }
        respResult=selectAll();

        return respResult;
    }
    /**
     * 根据点击图标删除所在的行
     * @param carQo
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteByid",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public RespResult updateByCarId(CarQo carQo){
        int i = carService.updateByCarId(carQo);
        RespResult respResult=null;
        if (i==-1){
            respResult.setMessage("删除失败");
        }
        respResult=selectAll();
        return respResult;
    }
}
